package com.qdsgjsfk.vision.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.qdsgjsfk.vision.R;
import com.rest.response.StudentOtherResponse;

import java.util.List;

public class MultipleAdapter extends RecyclerView.Adapter<MultipleAdapter.Holder> {

    private Context context;
    private List<StudentOtherResponse.MultipleChoiceQuery> list;
    private OnItemSelectListener onItemSelectListener;
    private int isDistinguishLrEye;

    public MultipleAdapter(Context context, List<StudentOtherResponse.MultipleChoiceQuery> list, int isDistinguishLrEye, OnItemSelectListener onItemSelectListener) {
        this.context = context;
        this.list = list;
        this.onItemSelectListener = onItemSelectListener;
        this.isDistinguishLrEye = isDistinguishLrEye;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new Holder(LayoutInflater.from(context).inflate(R.layout.item_multiple, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        if (isDistinguishLrEye == 0) {
            if (list.get(position).odChoosed == 1) {
                holder.tv_name.setTextColor(context.getResources().getColor(R.color.mainColor));
                onItemSelectListener.onItemInit(View.VISIBLE);
            } else {
                holder.tv_name.setTextColor(context.getResources().getColor(R.color.commonGrey));
                onItemSelectListener.onItemInit(View.GONE);
            }
            holder.tv_name.setText(list.get(position).paramName);
            holder.tv_name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (list.get(position).odChoosed == 1) {
                        list.get(position).odChoosed = 0;
                    } else {
                        if (list.get(position).isExclusiveness == 1) {
                            for (StudentOtherResponse.MultipleChoiceQuery multipleChoiceQuery : list) {
                                multipleChoiceQuery.odChoosed = 0;
                            }
                        } else {
                            for (StudentOtherResponse.MultipleChoiceQuery multipleChoiceQuery : list) {
                                if (multipleChoiceQuery.isExclusiveness == 1) {
                                    multipleChoiceQuery.odChoosed = 0;
                                }
                            }
                        }
                        list.get(position).odChoosed = 1;
                    }
                    StringBuffer callback = new StringBuffer();
                    for (StudentOtherResponse.MultipleChoiceQuery multipleChoiceQuery : list) {
                        if (multipleChoiceQuery.odChoosed == 1) {
                            callback.append(multipleChoiceQuery.paramName+",");
                        }
                        if (multipleChoiceQuery.isNeedRemark == 1 && multipleChoiceQuery.odChoosed == 1) {
                            onItemSelectListener.onItemSelect(callback.toString(), View.VISIBLE);
                        } else {
                            onItemSelectListener.onItemSelect(callback.toString(), View.GONE);
                    }
                    }
                    notifyDataSetChanged();
                }
            });
        }else{
            if (list.get(position).osChoosed == 1) {
                holder.tv_name.setTextColor(context.getResources().getColor(R.color.mainColor));
            } else {
                holder.tv_name.setTextColor(context.getResources().getColor(R.color.commonGrey));
            }
            holder.tv_name.setText(list.get(position).paramName);
            holder.tv_name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (list.get(position).osChoosed == 1) {
                        list.get(position).osChoosed = 0;
                    } else {
                        if (list.get(position).isExclusiveness == 1) {
                            for (StudentOtherResponse.MultipleChoiceQuery multipleChoiceQuery : list) {
                                multipleChoiceQuery.osChoosed = 0;
                            }
                        } else {
                            for (StudentOtherResponse.MultipleChoiceQuery multipleChoiceQuery : list) {
                                if (multipleChoiceQuery.isExclusiveness == 1) {
                                    multipleChoiceQuery.osChoosed = 0;
                                }
                            }
                        }
                        list.get(position).osChoosed = 1;
                    }
                    StringBuffer callback = new StringBuffer();
                    for (StudentOtherResponse.MultipleChoiceQuery multipleChoiceQuery : list) {
                        if (multipleChoiceQuery.osChoosed == 1) {
                            callback.append(multipleChoiceQuery.paramName+",");
                        }
                        if (multipleChoiceQuery.isNeedRemark == 1 && multipleChoiceQuery.osChoosed == 1) {
                            onItemSelectListener.onItemSelect(callback.toString(), View.VISIBLE);
                        } else {
                            onItemSelectListener.onItemSelect(callback.toString(), View.GONE);
                        }
                    }
                    notifyDataSetChanged();
                }
            });
        }
    }



    @Override
    public int getItemCount() {
        return list.size();
    }


    class Holder extends RecyclerView.ViewHolder {
        TextView tv_name;

        public Holder(View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
        }
    }

    public interface OnItemSelectListener {
        void onItemSelect(String para, int isVisible);

        void onItemInit(int isVisible);
    }
}
